package com.wushi.lenovo.asia5b.app_ui.credit.activity;

import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.mvp.base.BaseAppCompatActivity;
import com.mvp.base.BaseFragmentPagerAdapter;
import com.mvp.base.model.ResultModel;
import com.wushi.lenovo.asia5b.R;
import com.wushi.lenovo.asia5b.app_event.TopupEvent;
import com.wushi.lenovo.asia5b.app_model.credit.bean.TopupType;
import com.wushi.lenovo.asia5b.app_ui.credit.fragment.CardFragment;
import com.wushi.lenovo.asia5b.app_ui.credit.fragment.FpxFragment;
import com.wushi.lenovo.asia5b.app_ui.other.dialog.pay.PayResultDialog;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by zhangyuncai on 2019/1/15.
 * 充值
 */
public class TopupActivity extends BaseAppCompatActivity {
    @BindView(R.id.ll_topup_fpx)
    LinearLayout ll_topup_fpx;
    @BindView(R.id.ll_topup_card)
    LinearLayout ll_topup_card;
    @BindView(R.id.ll_topup_atm)
    LinearLayout ll_topup_atm;
    @BindView(R.id.vp_topup)
    ViewPager mViewPager;
    @BindView(R.id.tv_topup_fpxtext)
    TextView tv_topup_fpxtext;
    @BindView(R.id.tv_topup_fpxicon)
    TextView tv_topup_fpxicon;
    @BindView(R.id.tv_topup_cardtext)
    TextView tv_topup_cardtext;
    @BindView(R.id.tv_topup_cardicon)
    TextView tv_topup_cardicon;
    @BindView(R.id.tv_topup_atmtext)
    TextView tv_topup_atmtext;
    @BindView(R.id.tv_topup_atmicon)
    TextView tv_topup_atmicon;
    @BindView(R.id.ll_topup_fpxicon)
    LinearLayout ll_topup_fpxicon;
    @BindView(R.id.ll_topup_cardicon)
    LinearLayout ll_topup_cardicon;
    @BindView(R.id.ll_topup_atmtext)
    LinearLayout ll_topup_atmtext;
    @BindView(R.id.tv_topup_transfer)
    public TextView tv_topup_transfer;
    private TopupType topupType = TopupType.fpx;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_topup;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EventBus.getDefault().register(this);
        initView();
        initData();
        initEvent();
    }

    @Override
    protected void initView() {
        changeType();


        List<Fragment> fragments = new ArrayList<>();
        fragments.add(FpxFragment.sNewStance(this, FpxFragment.class.getName()));
        fragments.add(CardFragment.sNewStance(this, CardFragment.class.getName()));
//        fragments.add(AtmEbankFragment.newInstance(this));
        mViewPager.setAdapter(new BaseFragmentPagerAdapter(getSupportFragmentManager(), fragments));
        mViewPager.setOffscreenPageLimit(3);
    }

    private void changeType() {
        ll_topup_fpx.setSelected(topupType == TopupType.fpx);
        ll_topup_card.setSelected(topupType == TopupType.card);
        ll_topup_atm.setSelected(topupType == TopupType.atm);

        tv_topup_fpxtext.setSelected(topupType == TopupType.fpx);
        tv_topup_fpxicon.setSelected(topupType == TopupType.fpx);
        tv_topup_cardtext.setSelected(topupType == TopupType.card);
        tv_topup_cardicon.setSelected(topupType == TopupType.card);
        tv_topup_atmtext.setSelected(topupType == TopupType.atm);
        tv_topup_atmicon.setSelected(topupType == TopupType.atm);

        ll_topup_fpxicon.setVisibility(topupType == TopupType.fpx ? View.VISIBLE : View.GONE);
        ll_topup_cardicon.setVisibility(topupType == TopupType.card ? View.VISIBLE : View.GONE);
        ll_topup_atmtext.setVisibility(topupType == TopupType.atm ? View.VISIBLE : View.GONE);
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void initEvent() {
        mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int i, float v, int i1) {

            }

            @Override
            public void onPageSelected(int i) {
                topupType = i == 0 ? TopupType.fpx : i == 1 ? TopupType.card : TopupType.atm;
                changeType();
            }

            @Override
            public void onPageScrollStateChanged(int i) {

            }
        });
    }

    @OnClick({
            R.id.ll_topup_fpx,
            R.id.ll_topup_card,
            R.id.ll_topup_atm
    })
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.ll_topup_fpx:
                mViewPager.setCurrentItem(0);
                break;
            case R.id.ll_topup_card://信用卡
                mViewPager.setCurrentItem(1);
                break;
            case R.id.ll_topup_atm:
                mViewPager.setCurrentItem(2);
                break;
            default:
                break;
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN) //在ui线程执行
    public void onTopupEvent(TopupEvent event) {
        if (event != null&&event.resultModel!=null) {
            ResultModel resultModel = event.resultModel;
            PayResultDialog dialog = new PayResultDialog(mActivity, resultModel, false);
            dialog.show();
            if (resultModel.isSuccess()) {
                dialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
                    @Override
                    public void onDismiss(DialogInterface dialog) {
                        finish();
                    }
                });
            }
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}
